<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class School extends MY_Controller {

	public $left_menu_min;

	function __construct(){
		parent::__construct();

		$this->left_menu_min = array("menu" => "网校设置", "curr" => '/school', "list" => array("学校设置" => "/school", "学校资讯" => "/news", "帮助中心" => "/help", "友情连接" => "/friend_link", "网点管理" => "/wangdian", "问答管理" => "/question_topic", "客服设置" => "/kefu"));
		  $this->smarty->assign("left_menu_min", $this->left_menu_min);
		  $this->smarty->assign("menu", 'school');

	}



	/**
	 * Index Page for this controller.
	 *
	 */
	public function index()
	{
		$this->load->model("School_menus");
		$post_data = $this->input->post();
		if($post_data){
			$have_menu = $this->School_menus->get_school_menus($this->user_data['school_id']);
			$have_menu_id = array_column($have_menu, "id");
			$add_ids = array_diff($have_menu_id, $post_data['menu']);
			if(count($add_ids) > 0){
				$add_menus = array();
				foreach ($add_ids as $key => $val) {
					$add_menus[] = array("menu_id" => $val, "school_id" => $this->user_data['school_id']);
				}
				$this->db->insert_batch("o2o_school_notshow_menus", $add_menus);
			}
			$del_ids = array_diff($post_data['menu'], $have_menu_id);
			if(count($del_ids) > 0){
				$this->db->where("school_id", $this->user_data['school_id']);
				$this->db->where_in("menu_id", $del_ids);
				$this->db->delete("o2o_school_notshow_menus");
			}
			show_message($this->jump_url);
		}else{
	        $menu_list = $this->School_menus->get_all_menu($this->user_data['school_id']);
	        $menu_list_data = array();
	        if(count($menu_list) > 0){
	            foreach ($menu_list as $key => $val) {
	                if($val['pid'] == 0){
	                    $menu_list_data[$val['id']]['info'] = $val;
	                }else{
	                    $menu_list_data[$val['pid']]['sub'][] = $val;
	                }
	            }
	        }
	        $this->smarty->assign("menu_list", $menu_list_data);
			$this->smarty->display("school/index.html");
		}
	}

	function modulars(){
		$this->load->model("School_page_modulars");
		$post_data = $this->input->post();
		if($post_data){
			$have_modular = $this->School_page_modulars->get_school_page_modulars($this->user_data['school_id']);
			$have_modular_id = array_column($have_modular, "id");
			$add_ids = array_diff($have_modular_id, isset($post_data['modular']) ? $post_data['modular'] : array());
			if(count($add_ids) > 0){
				$add_modulars = array();
				foreach ($add_ids as $key => $val) {
					$add_modulars[] = array("mid" => $val, "school_id" => $this->user_data['school_id']);
				}
				$this->db->insert_batch("o2o_school_notshow_page_modulars", $add_modulars);
			}
			$del_ids = array_diff(isset($post_data['modular']) ? $post_data['modular'] : array(), $have_modular_id);
			if(count($del_ids) > 0){
				$this->db->where("school_id", $this->user_data['school_id']);
				$this->db->where_in("mid", $del_ids);
				$this->db->delete("o2o_school_notshow_page_modulars");
			}
			show_message($this->jump_url);
		}else{
			$modulars_list = $this->School_page_modulars->get_all_school_page_modulars($this->user_data['school_id']);
			$modulars_data = array();
			foreach ($modulars_list as $key => $value) {
				$modulars_data[$value['type']][] = $value;
			}
			$this->smarty->assign("modulars_list", $modulars_data);
			$this->smarty->display("school/page_modulars.html");
		}
		
	}

	function info(){
		$school_info = get_field_by_where("o2o_school", "*", "id='{$this->user_data['school_id']}'");
		if(!$school_info){
			show_error('登录失效');exit;
		}
		$post_data = $this->input->post();
		if($post_data){
			if(!empty($post_data['school_logo']) && !strstr($post_data['school_logo'], 'http://') ){
				$post_data['school_logo'] = get_qiniu_url($post_data['school_logo']);
			}
			if(!empty($post_data['wx_mp_qrcode']) && !strstr($post_data['wx_mp_qrcode'], 'http://') ){
				$post_data['wx_mp_qrcode'] = get_qiniu_url($post_data['wx_mp_qrcode']);
			}
			if(!empty($post_data['wx_manager_qrcode']) && !strstr($post_data['wx_manager_qrcode'], 'http://') ){
				$post_data['wx_manager_qrcode'] = get_qiniu_url($post_data['wx_manager_qrcode']);
			}
			$this->db->where("id", $this->user_data['school_id']);
			$this->db->update("o2o_school", $post_data);
			show_message($this->jump_url);
		}else{
			$this->load->model("Areas");
			$province_list = $this->Areas->get_province_data();
			if($school_info['province_code'] > 0){
				$city_list = $this->Areas->get_province_data($school_info['province_code']);
				$this->smarty->assign("city_list", $city_list);
			}

			if($school_info['city_code'] > 0){
				$area_list = $this->Areas->get_province_data($school_info['city_code']);
				$this->smarty->assign("area_list", $area_list);
			}
			$this->smarty->assign("province_list", $province_list);
			$this->smarty->assign("info", $school_info);
			$this->smarty->display("school/info.html");
		}
	}

	function wx_pay_config(){
		$wx_config_info = get_field_by_where("o2o_school_wx_config", "*", "school_id = '{$this->user_data['school_id']}'");
		$post_data = $this->input->post();
		
		if($post_data){
			/*if(!empty($post_data['apiclient_cert'])){
				$post_data['apiclient_cert'] = get_qiniu_url($post_data['apiclient_cert']);
			}
			if(!empty($post_data['apiclient_key'])){
				$post_data['apiclient_key'] = get_qiniu_url($post_data['apiclient_key']);
			}*/
			if($wx_config_info){
				$this->db->where("school_id", $this->user_data['school_id']);
				$this->db->update("o2o_school_wx_config", $post_data);
			}else{
				$post_data['school_id'] = $this->user_data['school_id'];
				$this->db->insert("o2o_school_wx_config", $post_data);
			}
			show_message($this->jump_url);exit;
		}
		$this->smarty->assign("apiclient_cert", md5($this->user_data['school_id'] . 'apiclient_cert.pem'));
		$this->smarty->assign("apiclient_key", md5($this->user_data['school_id'] . 'apiclient_key.pem'));
		$this->smarty->assign("info", $wx_config_info);
		$this->smarty->display("school/wx_pay_config.html");
	}
}
